/**
 * 
 */
package com.asset.salesforce.controller.ajax;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.domain.City;
import com.asset.salesforce.service.CityService;

// TODO: Auto-generated Javadoc
/**
 * The Class CityRestController.
 *
 * @author Hisham
 */
@Controller
public class CityRestController {

	/** The city service. */
	@Autowired
	private CityService cityService;
	
	/**
	 * Gets the lead cities.
	 *
	 * @param model the model
	 * @param session the session
	 * @param request the request
	 * @return the lead cities
	 */
	@RequestMapping(value = "/leadFormCityCountry", method = RequestMethod.POST)
	public @ResponseBody
	String getLeadCities(Model model, HttpSession session,HttpServletRequest request) {
		Integer selectedCountry = Integer.parseInt(request.getParameter("selectedCountry").toString());
		List<City> cities = new ArrayList<City>();
		cities = cityService.getAllCityByCountryId(selectedCountry);
		String cityIds = "";
		String cityNames = "";
		for (City city : cities) {
			if (cities.size() - 1 == cities.indexOf(city)) {

				cityIds += (city.getId()).toString();
				cityNames += city.getName();
			} else {
				cityIds += (city.getId()).toString();
				cityIds += "?";
				cityNames += city.getName();
				cityNames += "?";
			}
		}
		String citiesList = cityIds + "***" + cityNames;
		return citiesList;

	}
}
